﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xf.Entitys;

namespace xf.Service
{
    class GoodsService
    {
        private static GoodsService goodsService = null;

        /// <summary>
        /// 产品服务层
        /// </summary>
        /// <returns></returns>
        public static GoodsService GetInstance()
        {
            if (goodsService == null)
            {
                goodsService = new GoodsService();
            }
            return goodsService;
        }

        /// <summary>
        /// 获取查询对象
        /// </summary>
        /// <param name="queryParam"></param>
        /// <param name="like"></param>
        /// <returns></returns>
        public ISugarQueryable<GOODS_NAME> GetSugarQueryable(Dictionary<string, string> queryParam, bool like = true) {
            ISugarQueryable<GOODS_NAME> iSugarQueryable = DbService.GetSqlSugarClient()
                .Queryable<GOODS_NAME>()
                .OrderBy(row => row.Goods_id, OrderByType.Desc);
            queryParam.TryGetValue("Goods_id", out string goodsId);
            queryParam.TryGetValue("Goods_name", out string goodsName);
            if (!string.IsNullOrEmpty(goodsId))
            {
                if (like)
                {
                    iSugarQueryable
                        .Where(row =>
                            (row.Goods_id == goodsId) ||
                            row.Goods_id.Contains(goodsId) ||
                            row.Goods_id.StartsWith(goodsId) ||
                            row.Goods_id.EndsWith(goodsId)
                        );
                }
                else
                {
                    iSugarQueryable.Where(row => row.Goods_id == goodsId);
                }
            }
            if (!string.IsNullOrEmpty(goodsName))
            {
                if (like)
                {
                    iSugarQueryable
                        .Where(row =>
                            (row.Goods_name == goodsName) ||
                            row.Goods_name.Contains(goodsName) ||
                            row.Goods_name.StartsWith(goodsName) ||
                            row.Goods_name.EndsWith(goodsName)
                        );
                }
                else
                {
                    iSugarQueryable.Where(row => row.Goods_name == goodsName);
                }

            }
            return iSugarQueryable;
        }

        public List<GOODS_NAME> All(Dictionary<string, string> queryParam, bool like = true) 
        {
            ISugarQueryable<GOODS_NAME> iSugarQueryable = GetSugarQueryable(queryParam, like);
            return iSugarQueryable.ToList();
        }

        /// <summary>
        /// 查询对应产品信息，返回单条
        /// </summary>
        /// <param name="goodsNameObj"></param>
        /// <param name="like"></param>
        /// <returns></returns>
        public GOODS_NAME One(GOODS_NAME goodsNameObj, bool like = true)
        {
            Dictionary<string, string> queryParam = new Dictionary<string, string> {
                { "Goods_id", goodsNameObj.Goods_id},
                { "Goods_name", goodsNameObj.Goods_name},
            };
            ISugarQueryable<GOODS_NAME> iSugarQueryable = GetSugarQueryable(queryParam, like);
            return iSugarQueryable.ToList().FirstOrDefault();
        }

        /// <summary>
        /// 添加用户操作
        /// </summary>
        /// <param name="param"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool Add(GOODS_NAME goodsNameObj, ref Dictionary<string, string> detail)
        {
            bool result = false;
            if (string.IsNullOrEmpty(goodsNameObj.Goods_id) || string.IsNullOrEmpty(goodsNameObj.Goods_name))
            {
                detail.Add("msg", "产品编码或产品名称不能为空");
                return result;
            }
            GOODS_NAME goodsResult = One(goodsNameObj, false);
            if (goodsResult != null)
            {
                detail.Add("msg", "该商品已存在");
                return result;
            }
            result = (DbService.GetSqlSugarClient().Insertable(goodsNameObj).ExecuteCommand() == 1);
            return result;
        }

        /// <summary>
        /// 删除指定产品
        /// </summary>
        /// <param name="goodsNameObj"></param>
        /// <returns></returns>
        public bool Delete(GOODS_NAME goodsNameObj) 
        {
            bool result = false;
            result = DbService.GetSqlSugarClient().Deleteable<GOODS_NAME>(goodsNameObj).ExecuteCommand() == 1;
            return result;
        }
    }
}
